//
//  XWmenu.m
//  网易新闻
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "XWmenu.h"
#import "SearchController.h"
#import "Settings.h"
#import "CollectionController.h"
#import "SDImageCache.h"
@interface XWmenu()
@property(nonatomic, assign)CGRect sizeClose;
@property(nonatomic, assign)CGRect sizePop;
@property(nonatomic, retain)UIButton *search;
@property(nonatomic, retain)UIButton *night;
@property(nonatomic, retain)UIButton *collection;
@property(nonatomic, retain)UIButton *clear;
@end
@implementation XWmenu
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.hidden = YES;
        [self setUserInteractionEnabled:NO];
        self.backgroundColor = [UIColor whiteColor];
        self.alpha = 0.9;
        self.sizeClose = CGRectMake(frame.size.width, -frame.size.height, frame.size.width, frame.size.height);
        self.sizePop = CGRectMake(0, 64, frame.size.width, frame.size.height);
//        [self notificationHandle];
        [self addButton];
    }
    return self;
}

- (void)addButton{
    UIButton *weather = [UIButton buttonWithType:UIButtonTypeCustom];
    weather.frame = CGRectMake(0, 0, 375, 250);
    [weather setImage:[[UIImage imageNamed:[[NSBundle mainBundle]pathForResource:@"S51012-192720" ofType:@"jpg"]]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [weather addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:weather];
    
    self.search = [UIButton buttonWithType:UIButtonTypeSystem];
    self.search.frame = CGRectMake(45, 250, 120, 120);
    self.search.layer.masksToBounds = YES;
    self.search.layer.cornerRadius = 60;
    [self.search setTitle:@"新闻\n搜索" forState:UIControlStateNormal];
    self.search.titleLabel.numberOfLines = 0;
    self.search.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.search setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.search setBackgroundColor:[UIColor orangeColor]];
    [self.search addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.search];
    
    self.night = [UIButton buttonWithType:UIButtonTypeSystem];
    self.night.frame = CGRectMake(210, 250, 120, 120);
    self.night.layer.masksToBounds = YES;
    self.night.layer.cornerRadius = 60;
    self.night.titleLabel.font = [UIFont systemFontOfSize:30];
    self.night.titleLabel.numberOfLines = 0;
    [self.night setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.night setBackgroundColor:[UIColor blueColor]];
    [self.night addTarget:self action:@selector(night:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.night];
    
    self.collection = [UIButton buttonWithType:UIButtonTypeSystem];
    self.collection.frame = CGRectMake(45, 415, 120, 120);
    self.collection.layer.masksToBounds = YES;
    self.collection.layer.cornerRadius = 60;
    [self.collection setTitle:@"我的\n收藏" forState:UIControlStateNormal];
    self.collection.titleLabel.font = [UIFont systemFontOfSize:30];
    self.collection.titleLabel.numberOfLines = 0;
    [self.collection setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.collection setBackgroundColor:[UIColor purpleColor]];
    [self.collection addTarget:self action:@selector(collection:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.collection];
    
    self.clear = [UIButton buttonWithType:UIButtonTypeSystem];
    self.clear.frame = CGRectMake(210, 415, 120, 120);
    self.clear.layer.masksToBounds = YES;
    self.clear.layer.cornerRadius = 60;
    [self.clear setTitle:@"清理\n缓存" forState:UIControlStateNormal];
    self.clear.titleLabel.numberOfLines = 0;
    self.clear.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.clear setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.clear setBackgroundColor:[UIColor cyanColor]];
    [self.clear addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.clear];
}

- (void)popView{
    [self setNightTitle:self.night];
    if (self.hidden) {
        self.hidden = NO;
        [self.superview bringSubviewToFront:self];
        self.frame = self.sizeClose;
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = self.sizePop;
        }];
        [self setUserInteractionEnabled:YES];
    }else{
        [self closeView];
    }
}

- (void)closeView{
    [self setUserInteractionEnabled:NO];
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = self.sizeClose;
    }completion:^(BOOL finished) {
        if (finished) {
            self.hidden = YES;
        }
    }];
}

- (void)search:(UIButton *)button{
    [self closeView];
    SearchController *search = [[SearchController alloc]init];
    [self.navi pushViewController:search animated:YES];
    [search release];
}
- (void)night:(UIButton *)button{
    [Settings shareSettings].isNight = ![Settings shareSettings].isNight;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeBackgroundColor" object:nil userInfo:nil];
    [self setNightTitle:button];
    [self closeView];
}
- (void)collection:(UIButton *)button{
    [self closeView];
    CollectionController *collection = [[CollectionController alloc]init];
    [self.navi pushViewController:collection animated:YES];
    [collection release];
}
- (void)clear:(UIButton *)button{
    [[Settings shareSettings]closeDB];
    [[SDImageCache sharedImageCache] clearDisk];
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager removeItemAtPath:cachesPath error:nil];
    [self closeView];
}

- (void)setNightTitle:(UIButton *)button{
    if ([Settings shareSettings].isNight) {
        [button setTitle:@"日间\n模式" forState:UIControlStateNormal];
    }else{
        [button setTitle:@"夜间\n模式" forState:UIControlStateNormal];
    }
}

//- (void)notificationHandle{
//    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
//        self.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
//    }];
//}

- (void)dealloc{
    [_navi release];
    [super dealloc];
}
@end
